/**
 * @file ui_info_list.c
 * @author your name (you@domain.com)
 * @brief info list
 * @version 0.1
 * @date 2023-06-17
 * 
 * @copyright Copyright (c) 2023
 * 
 */
#include "ui_info_list.h"
#include "ui_change.h"

LV_IMG_DECLARE(back_icon);
LV_FONT_DECLAER(lv_font_montserrat_18);

static void icon_img_cb(lv_event_t *event)
{
       easy_ui_pub_event(TOPIC_BACK_UI, NULL, 0, 0);
}

lv_obj_t *add_top_title(lv_obj_t * obj, char*title_text)
{
    lv_obj_t *title = lv_obj_create(obj);
    if(title == NULL)
    {
        COMLOG_ERROR_LOG("title is null\n");
        return NULL;
    }

    lv_disp_t *d_disp = lv_disp_get_default();
    lv_obj_set_size(title, LV_PCT(100), d_disp->driver->ver_res*INFO_LIST_TILE_HEIGHT_RATE);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, d_disp->driver->ver_res*TOP_UI_HEIGHT_RATE + 5);
    lv_obj_clear_flag(title, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_radius(title, 0, 0);
    lv_obj_set_style_border_width(title, 0, 0);  
    lv_obj_set_style_bg_color(title, lv_color_hex(HOME_UI_BG_COLOR), 0);
    lv_obj_set_style_pad_top(title, 0, 0);
    lv_obj_set_style_pad_bottom(title, 0, 0);
    lv_obj_set_style_pad_left(title, 0, 0);
    lv_obj_set_style_pad_right(title, 0, 0);

    lv_obj_t* label_title = lv_label_create(title);
    if(label_title == NULL)
    {
        COMLOG_ERROR_LOG("label_title is null\n");
        lv_obj_del(title);
        return NULL;
    }
    lv_obj_set_style_text_color(label_title, lv_color_hex(INFO_LIST_TITLE_COLOR), 0);
    lv_label_set_text(label_title, title_text);
    lv_obj_align(label_title, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_text_align(label_title, LV_TEXT_ALIGN_CENTER, 0);  
    lv_obj_set_style_text_font(label_title, &lv_font_montserrat_18, 0);

    lv_obj_t* icon_img = lv_img_create(title);
    if(icon_img == NULL)
    {
        COMLOG_ERROR_LOG("icon_img is null\n");
        lv_obj_del(title);
        return NULL;
    } 

    lv_img_set_src(icon_img, &back_icon);
    lv_obj_align(icon_img, LV_ALIGN_LEFT_MID, 5, 0);
    lv_obj_add_flag(icon_img, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(icon_img, icon_img_cb, LV_EVENT_CLICKED, NULL);   

    return title;
}

lv_obj_t *ui_info_list_create(lv_obj_t *parent, char*title_text)
{
    lv_obj_t *title_obj = add_top_title(parent, title_text);
    if(title_obj == NULL)
    {
        return NULL;
    }
    lv_obj_t *list = lv_list_create(parent);
    if(list == NULL)
    {
        lv_obj_del(title_obj);
        return NULL;
    }
    lv_obj_set_style_pad_top(list, 0, 0);
    lv_obj_set_style_pad_bottom(list, 0, 0);
    lv_obj_set_style_pad_left(list, 0, 0);
    lv_obj_set_style_pad_right(list, 0, 0);
    lv_disp_t *d_disp = lv_disp_get_default();
    lv_obj_set_size(list, LV_PCT(100), d_disp->driver->ver_res*(1-TOP_UI_HEIGHT_RATE-INFO_LIST_TILE_HEIGHT_RATE));
    lv_obj_set_y(list, d_disp->driver->ver_res*(TOP_UI_HEIGHT_RATE+INFO_LIST_TILE_HEIGHT_RATE) + 5);
    // lv_obj_set_style_pad_left(list , 28 , 0);
    // lv_obj_set_style_pad_right(list, 28, 0);
    lv_obj_add_flag(list, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_radius(list, 0, 0);
    lv_obj_set_style_border_width(list, 0, 0);     
    lv_obj_set_style_bg_color(list, lv_color_hex(HOME_UI_BG_COLOR), 0);
    return list;
}

int ui_info_item_add_to_list(lv_obj_t *parent_list,  char *up_label_text, char *down_label_text)
{
    lv_obj_t* obj = lv_obj_create(parent_list);
    lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_bg_color(obj, lv_color_hex(HOME_UI_BG_COLOR), 0);
    lv_obj_set_style_radius(obj, 0 ,0);    
    lv_obj_set_style_border_width(obj , 0 ,0);
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_width(obj, LV_PCT(100));

    lv_obj_t* label_up = lv_label_create(obj);
    if(label_up == NULL)
    {
       COMLOG_ERROR_LOG("label_up is null\n");
       return -1;
    }
    lv_obj_set_style_text_font(label_up, &lv_font_montserrat_18, 0);
    lv_label_set_text(label_up, up_label_text);
    lv_label_set_long_mode(label_up, LV_LABEL_LONG_WRAP);
    lv_obj_set_style_text_color(label_up, lv_color_hex(INFO_LIST_UP_LABEL_COLOR), 0);
    lv_obj_set_width(label_up, LV_PCT(100));
    lv_obj_set_style_text_align(label_up, LV_TEXT_ALIGN_LEFT, 0);


    lv_obj_t* label_down = lv_label_create(obj);
    if(label_down == NULL)
    {
       COMLOG_ERROR_LOG("label_down is null\n");
       lv_obj_del(label_down);
       return -1;
    }
    lv_obj_set_style_text_font(label_down, &lv_font_montserrat_16, 0);
    lv_label_set_text(label_down, down_label_text);
    lv_label_set_long_mode(label_down, LV_LABEL_LONG_WRAP);
    lv_obj_set_style_text_color(label_down, lv_color_hex(INFO_LIST_DOWN_LABEL_COLOR), 0);
    lv_obj_set_width(label_down, LV_PCT(100));
    lv_obj_set_style_text_align(label_down, LV_TEXT_ALIGN_LEFT, 0);

    lv_obj_t* obj_line = lv_obj_create(obj);
    lv_obj_set_size(obj_line, LV_PCT(100), INFO_LIST_DOWN_LINE_WIDTH);
    lv_obj_set_style_bg_color(obj_line, lv_color_hex(INFO_LIST_DOWN_LINE_COLOR), 0);
    lv_obj_set_size(obj, LV_PCT(100), INFO_LIST_ITEM_HEIGHT);
    return 0;
}